package org.geoscrape;

import static org.geoscrape.util.HtmlParser.getContent;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;

import org.geoscrape.util.HtmlParser;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Defines a log for a cache.
 * 
 */
public class CacheLog implements Serializable
{
	private static final long serialVersionUID = 3665815642473663594L;
	private long logTime;// logged time in UTC
	private Cacher loggedBy;
	private String text;
	private LogType logType;
	private Long id;

	/**
	 * Default Constructor.
	 * @throws JSONException 
	 * @throws ParseException 
	 */
	public CacheLog(JSONObject data,Login login) throws JSONException, ParseException
	{
		logType = LogType.parse(data.getString("LogType"));
		TimeZone laTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
		SimpleDateFormat sdf = new SimpleDateFormat(login.getDateFormat(),Locale.ENGLISH);
		sdf.setTimeZone(laTimeZone);
		logTime = sdf.parse(data.getString("Visited")).getTime();
		loggedBy = new Cacher(HtmlParser.convertHtmlChar(data.getString("UserName")), "http://www.geocaching.com/profile/?guid="+data.getString("AccountGuid"));
		loggedBy.setFoundCaches(data.getLong("GeocacheFindCount"));
		loggedBy.setId(data.getLong("AccountID"));
		
		setId(data.getLong("LogID"));
		// get the log text
		text = HtmlParser.convertHtmlChar( data.getString("LogText")).trim();
	}
	
	/**
	 * Parse a log from the HTML description.
	 * 
	 * @param logDescription
	 * @throws ParseException
	 */
	public CacheLog(String logDescription,Login login) throws ParseException
	{
		// get the log type
		String type = getContent("title=\"", "\"", getContent("div class=\"HalfLeft LogType\">","</div>",logDescription));
		logType = LogType.parse(type);
		// get the log date
		String logDate = getContent("<span class=\"minorDetails LogDate\">", "</span>", logDescription);
		TimeZone laTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
		SimpleDateFormat sdf = new SimpleDateFormat(login.getDateFormat(),Locale.ENGLISH);
		sdf.setTimeZone(laTimeZone);
		logTime = sdf.parse(logDate).getTime();
		// get the logger profile url
		String loggerUrl = getContent("<a href=\"", "\"", logDescription);
		// get the logger name
		String endBit = logDescription.substring(logDescription.indexOf("<a href=\""));
		String loggerName = getContent("\">", "</a", endBit);
		// get the logged count
		Long loggedCount = 0L;
		String loggedCountString = getContent("title=\"Caches Found\" />", "</div>", endBit);
		if(loggedCountString!=null)
		{			
			if(loggedCountString.contains("&nbsp"))
			{
				loggedCountString = loggedCountString.substring(0,loggedCountString.indexOf("&nbsp"));
			}
			loggedCountString = loggedCountString.replaceAll(",", "");
			loggedCount = Long
					.parseLong(loggedCountString.trim());
		}
		loggedBy = new Cacher(loggerName, loggerUrl);
		String idString = getContent("id=\"", "\"", endBit);
		if(idString != null)
		{
			setId(Long.parseLong(idString));
		}
		loggedBy.setFoundCaches(loggedCount);
		// get the log text
		text = getContent("<p class=\"LogText\">", "</p>",
				endBit);
		

	}

	/**
	 * @return the logTime
	 */
	public long getLogTime()
	{
		return logTime;
	}

	/**
	 * @param logTime
	 *            the logTime to set
	 */
	public void setLogTime(long logTime)
	{
		this.logTime = logTime;
	}

	/**
	 * @return the loggedBy
	 */
	public Cacher getLoggedBy()
	{
		return loggedBy;
	}

	/**
	 * @param loggedBy
	 *            the loggedBy to set
	 */
	public void setLoggedBy(Cacher loggedBy)
	{
		this.loggedBy = loggedBy;
	}

	/**
	 * @return the text
	 */
	public String getText()
	{
		return text;
	}

	/**
	 * @param text
	 *            the text to set
	 */
	public void setText(String text)
	{
		this.text = text;
	}

	/**
	 * @return the logType
	 */
	public LogType getLogType()
	{
		return logType;
	}

	/**
	 * @param logType
	 *            the logType to set
	 */
	public void setLogType(LogType logType)
	{
		this.logType = logType;
	}

	/**
	 * @return the id
	 */
	public Long getId()
	{
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id)
	{
		this.id = id;
	}
}
